﻿using CoreShop.Library;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Windows.Forms;

namespace CoreShop.App
{
    public partial class UIMultiButtons : UIUserControl
    {
        public UIMultiButtons()
        {
            InitializeComponent();
        }

        private Size itemSize = new Size(108, 40);
        public Size ItemSize
        {
            get { return itemSize; }
            set { itemSize = value; }
        }

        private Color fillColorXp = Color.White;
        public Color FillColorXp
        {
            get => fillColorXp;
            set
            {
                fillColorXp = value;
            }
        }

        public int FontSize = 12;

        public bool AutoWidth { get; set; }

        public UIButton SelectedBtn { get; set; }

        private int AutoIndex;

        public void Add(string text,long tag,bool isSelected = false)
        {
            UIButton btn = new UIButton();
            btn.Text = text;
            btn.Tag = tag;
            btn.Style = UIStyle.Orange;
            btn.Style = UIStyle.Custom;
            btn.StyleCustomMode = true;
            btn.Size = itemSize;
            btn.Font = new Font("宋体", FontSize);
            btn.Click += Btn_Click;
            btn.Selected = isSelected;
            btn.BackColor = System.Drawing.Color.White; ;
            btn.Cursor = System.Windows.Forms.Cursors.Hand;
            btn.FillColor = FillColorXp;
            btn.FillColor2 = System.Drawing.Color.FromArgb(((int)(((byte)(220)))), ((int)(((byte)(155)))), ((int)(((byte)(40)))));
            btn.FillHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(227)))), ((int)(((byte)(175)))), ((int)(((byte)(83)))));
            btn.FillPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(176)))), ((int)(((byte)(124)))), ((int)(((byte)(32)))));
            btn.FillSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(248)))), ((int)(((byte)(229)))));
            btn.ForeColor = System.Drawing.Color.Black;
            btn.ForeHoverColor = System.Drawing.Color.Black;
            btn.ForePressColor = System.Drawing.Color.Black;
            btn.ForeSelectedColor = System.Drawing.Color.Black;
            btn.RectColor = System.Drawing.Color.FromArgb(((int)(((byte)(238)))), ((int)(((byte)(238)))), ((int)(((byte)(238)))));
            btn.RectHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(227)))), ((int)(((byte)(175)))), ((int)(((byte)(83)))));
            btn.RectPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(176)))), ((int)(((byte)(124)))), ((int)(((byte)(32)))));
            btn.RectSelectedColor = System.Drawing.Color.FromArgb(((int)(((byte)(255)))), ((int)(((byte)(189)))), ((int)(((byte)(0)))));
            btn.GroupIndex = AutoIndex++;
            btn.Paint += Btn_Paint;

            if (AutoWidth)
            {
                Size sz = TextRenderer.MeasureText(text, Font);
                btn.Width = sz.Width + 30;
            }

            panel.Add(btn);
        }

        private void Btn_Paint(object sender, PaintEventArgs e)
        {
            UIButton btn = (UIButton)sender;
            if (!btn.Selected) return;
            using (var path = CreateRoundedRectanglePathWithoutTop(new Rectangle(0, 0, btn.Width, btn.Height), btn.Radius))
                e.Graphics.FillPath(Color.Orange, path, true);

            e.Graphics.DrawFontImage(61452, 18, Color.Black, new Rectangle(btn.Width - 18, 0, 20, 18));
        }

        private GraphicsPath CreateRoundedRectanglePathWithoutTop(Rectangle rect, int radius)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(rect.X + rect.Width - radius, rect.Y, radius, radius, 270, 90);
            path.AddLine(new Point(rect.X + rect.Width, rect.Y + 25), new Point(rect.X + rect.Width, rect.Y + 25));
            path.AddLine(new Point(rect.X + rect.Width - 25, rect.Y), new Point(rect.X + rect.Width - 25, rect.Y));
            return path;
        }


        private int maxSelected = 1;

        public int MaxSelected
        {
            get => maxSelected;
            set
            {
                maxSelected = value;
                var btns = panel.GetControls<UIButton>(true);
                foreach (var item in btns)
                {
                    item.Selected = false;
                }

                SelectedChanged?.Invoke(this, EventArgs.Empty);
            }
        }

        private void Btn_Click(object sender, System.EventArgs e)
        {
            UIButton btn = (UIButton)sender;
            SelectedBtn = btn;
            if (MaxSelected == 1)
            {
                var btns = panel.GetControls<UIButton>(true);
                foreach (var item in btns)
                {
                    if (item.Equals(btn)) continue;
                    item.Selected = false;
                }                
                btn.Selected = !btn.Selected;                
                SelectedChanged?.Invoke(this, EventArgs.Empty);
            }
            else
            {
                if (btn.Selected)
                {
                    btn.Selected = false;
                    SelectedChanged?.Invoke(this, EventArgs.Empty);
                    
                }
                else
                {
                    if (SelectedCount >= MaxSelected)
                    {
                        OnOverSelectedCount?.Invoke(this, EventArgs.Empty);
                    }
                    else
                    {
                        btn.Selected = true;
                        SelectedChanged?.Invoke(this, EventArgs.Empty);                        
                    }
                }
            }            
        }

        public event EventHandler SelectedChanged;
        public event EventHandler OnOverSelectedCount;

        public int SelectedCount
        {
            get
            {
                var btns = panel.GetControls<UIButton>(true).Where(p => p.Selected);
                return btns.Count(); 
            }
        }

        public string[] SelectedButtons
        {
            get
            {
                var btns = panel.GetControls<UIButton>(true).Where(p => p.Selected);
                List<string> buttons = new List<string>();
                foreach (var item in btns)
                {
                    buttons.Add(item.Text);
                }

                return buttons.ToArray();
            }
        }

        public long[] SelectedButtonsValues
        {
            get
            {
                var btns = panel.GetControls<UIButton>(true).Where(p => p.Selected);
                List<long> buttons = new List<long>();
                foreach (var item in btns)
                {
                    buttons.Add(CommFunc.ConvertToInt64(item.Tag));
                }

                return buttons.ToArray();
            }
        }

        public int[] SelectedButtonsInts
        {
            get
            {
                var btns = panel.GetControls<UIButton>(true).Where(p => p.Selected);
                List<int> buttons = new List<int>();
                foreach (var item in btns)
                {
                    buttons.Add(CommFunc.ConvertToInt32(item.Tag));
                }

                return buttons.ToArray();
            }
        }

        public void Clear()
        {
            panel.Clear();
            SelectedChanged?.Invoke(this, EventArgs.Empty);
        }
    }
}
